/* ============================================================================ */
/*                   Import Data and Set Global Paths                           */
/* ============================================================================ */

clear all // Clears all stored data and variables

********************************************************************************	
/* ============================================================================ */
/*               Load and Process FinTech/Rejected Data (Investment = 1)            */
/* ============================================================================ */

use "$output\rejected_psm_forinvestment1.dta", clear

/* Keep only observations within the time window */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))
keep if deltamonths>=-12 & deltamonths<=24

/* Generate loan-related variables */
gen loan = stloan + ltloan + bm + bi + oc
gen lloan = log(loan)

/* ============================================================================ */
/*               Create Time Relative Variable for Trend Analysis               */
/* ============================================================================ */

/* Convert deltamonths to a non-negative categorical variable */
replace delta = deltamonths + 12
capture lab_delta drop 

/* Assign labels to delta to reflect pre/post periods */
forvalues n = 0(1)36 {
	local displayed_value = `n' - 12
	label define lab_delta `n' "`displayed_value'", add
}
label val delta lab_delta

/* ============================================================================ */
/*                      Regression and Coefficient Plot                         */
/* ============================================================================ */

foreach var in lloan {
	/* Run fixed effects regression with clustering */
	qui eststo `var' : reghdfe `var' i1.accepted##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

	/* Figure 5a, right */
	/* Generate coefficient plot */
	coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
	ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
	omitted base keep(1.accepted#*) rename(1.accepted#*= "") vertical ///
	yline(0, lpattern(shortdash) lcolor(grey)) ///
	xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" ///
	18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
	ylabel(-0.2(0.2)0.5, nogrid) xsize(6) ysize(4) ///
	plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

	/* Save plots */
	graph save "Graph" "$figures/Figure_5ar_trend_relative_`var'_coefplot_psm_forinvestment_1_rejected.gph", replace
	graph export "$figures/Figure_5ar_trend_relative_`var'_coefplot_psm_forinvestment_1_rejected.pdf", replace
}

/* ============================================================================ */
/*                Load and Process FinTech/Rejected Data (Investment = 0)            */
/* ============================================================================ */

use "$output\rejected_psm_forinvestment0.dta", clear

/* Keep only observations within the time window */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))
keep if deltamonths>=-12 & deltamonths<=24

/* Generate loan-related variables */
gen loan = stloan + ltloan + bm + bi + oc
gen lloan = log(loan)

/* ============================================================================ */
/*                      Regression and Coefficient Plot                         */
/* ============================================================================ */

foreach var in lloan {
	/* Run fixed effects regression with clustering */
	qui eststo `var' : reghdfe `var' i1.accepted##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

	/* Figure 5b, right */
	/* Generate coefficient plot */
	coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
	ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
	omitted base keep(1.accepted#*) rename(1.accepted#*= "") vertical ///
	yline(0, lpattern(shortdash) lcolor(grey)) ///
	xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" ///
	18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
	ylabel(-0.4(0.2)0.4, nogrid) xsize(6) ysize(4) ///
	plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

	/* Save plots */
	graph save "Graph" "$figures/Figure_5br_trend_relative_`var'_coefplot_psm_forinvestment_0_rejected.gph", replace
	graph export "$figures/Figure_5br_trend_relative_`var'_coefplot_psm_forinvestment_0_rejected.pdf", replace
}

********************************************************************************
/* ============================================================================ */
/*                   Load and Process FinTech/Rejected Data (No Missing)              */
/* ============================================================================ */

use "$output\matched_sample_accepted_no_missing.dta", clear

/* Keep only observations within the time window */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))
keep if deltamonths>=-12 & deltamonths<=24

/* Generate loan-related variables */
gen loan = stloan + ltloan + bm + bi + oc
gen lloan = log(loan)

/* Generate post variable for pre/post comparisons */
drop post
gen post = deltamonths >= 0

/* ============================================================================ */
/*                      Regression and Coefficient Plot                         */
/* ============================================================================ */

	/* Figure IA D1b, right */
foreach var in lloan {
	eststo `var' : reghdfe `var' i1.accepted##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  
	
	coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
	ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
	omitted base keep(1.accepted#*) rename(1.accepted#*= "") vertical ///
	yline(0, lpattern(shortdash) lcolor(grey)) ///
	xlabel(2 "-11" 4 "-9" ... 36 "23", labsize(small)) ///
	ylabel(-0.2(0.1)0.3, nogrid) xsize(6) ysize(4) ///
	plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

	/* Save plots */
	graph save "Graph" "$figures/Figure_D1b_trend_relative_`var'_coefplot_psm_nomissing_0_rejected.gph", replace
	graph export "$figures/Figure_D1b_trend_relative_`var'_coefplot_psm_nomissing_0_rejected.pdf", replace
}
